package me.firstapp.manage.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.common.module.system.JsonOperator;
import me.firstapp.common.utils.StrUtils;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		String loginToken = (String) request.getSession().getAttribute("loginToken");
		if (StrUtils.isNULL(loginToken)) {
			request.getRequestDispatcher("/loginManage/login.html").forward(request, response);
			return false;
		} else {
			JsonOperator operator = (JsonOperator) request.getSession().getAttribute("login_operator_" + loginToken);
			if (operator == null) {
				request.getRequestDispatcher("/loginManage/login.html").forward(request, response);
				return false;
			} else {
				request.setAttribute("operator", operator);
				return true;
			}
		}

	}

}
